<template>
  <div class="system-user-container">
    <el-card shadow="hover">
      <div class="system-user-search mb15">
        <el-input clearable v-model="state.tableData.param.name" size="default" placeholder="请输入名称"
          style="max-width: 180px" @clear="getTableData"> </el-input>
        <el-button size="default" type="primary" class="ml10" @click="getTableData">
          <el-icon>
            <ele-Search />
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenAddTenant">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新增
        </el-button>
      </div>
      <el-table :data="state.tableData.data" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="tenantId" label="租户号" width="80" />
        <el-table-column prop="name" label="租户名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="status" label="租户状态" show-overflow-tooltip>
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.status">启用</el-tag>
            <el-tag type="info" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="startTime" label="开始时间" show-overflow-tooltip></el-table-column>
        <el-table-column prop="endTime" label="结束时间" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenEditTenant(scope.row)">修改</el-button>
            <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
        :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum" background
        v-model:page-size="state.tableData.param.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total">
      </el-pagination>
    </el-card>
    <TenantDialog ref="tenantDialogRef" @refresh="getTableData()" />
  </div>
</template>

<script setup name="tenant">
import { toRefs, reactive, onMounted, ref, defineAsyncComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
// 引入组件
const TenantDialog = defineAsyncComponent(() => import('/@/views/tenant/tenant/dialog.vue'));

import { useTenantApi } from "/@/api/tenant";

const tenantApi = useTenantApi()

// 定义变量内容
const tenantDialogRef = ref();

const state = reactive({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      name: '',
      pageNum: 1,
      pageSize: 10,
    },
  },
});


const getTableData = () => {
  let params = {
    "page": state.tableData.param.pageNum,
    "limit": state.tableData.param.pageSize,
    "name": state.tableData.param.name,
  }
  tenantApi.tenantPage(params).then(res => {
    state.tableData.data = res.data.list;
    state.tableData.total = res.data.count;
  })
}
// 打开新增弹窗
const onOpenAddTenant = () => {
  tenantDialogRef.value.openDialog("add");
};
// 打开修改弹窗
const onOpenEditTenant = (row) => {
  tenantDialogRef.value.openDialog("edit", row.tenantId);
};
// 删除用户
const onRowDel = (row) => {
  ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.name}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      tenantApi.tenantDelete(row.tenantId).then(() => {
        getTableData();
        ElMessage.success('删除成功');
      })
    })
    .catch(() => { });
};
// 分页改变
const onHandleSizeChange = (val) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});

</script>
